
import { goodsQuery } from "@src/sp-mall/api/good";
import router from "@src/router";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import base from "@src/store/base"
import { getPageConfig } from '@mall/api/homePage'


Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['storeId'],
    actions: []
  }, {
    store: base,
  }],

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '搜索店内商品',
    goodList: [],
    pageNum: 1,
    pageSize: 10,
    keywordsList: [],
    modules: [],
    noData: false,
    showKeywords: true,
    inputValue: "",
    pageId: null,
    login: false,
  },
  methods: {
    onPullDownRefresh() {
      console.log("下拉刷新")
      wx.stopPullDownRefresh({ success(e) { console.log('e', e) }, fail(e) { console.log('e', e) } });
      this.getPageConfigFunc(this.data.pageId);
    },
    onLogin() {
      this.setData({ login: true})
    },
    /**
     * 函数 方法
     */
    goIndex() {
      wx.redirectTo({ url: 'pages/index/index' })
    },
    //返回上个页面
    goback() {
      wx.navigateBack({
        delta: 1,
      })
    },
    searchInputFunc(detail) {
      if (detail.detail) {
        console.log('keywordsList', keywordsList)
        this.searchGoodsFunc(detail.detail);
        let keywordsList = wx.getStorageSync('keywords');
        if (!keywordsList) {
          let arr = [];
          arr.unshift(detail.detail);
          wx.setStorageSync('keywords', arr)
        } else {
          keywordsList.unshift(detail.detail);
          if (keywordsList.length > 10) {
            keywordsList = keywordsList.slice(0, 9);
          }
          wx.setStorageSync('keywords', keywordsList)
        }
        let temp = wx.getStorageSync('keywords');
        this.setData({
          keywordsList: temp
        })
      }
    },
    searchGoodsFunc(keyword) {
      if (this.data.noData) { return; };
      if (this.data.showKeywords) { this.setData({ showKeywords: false }) }
      let storeId = this.data.storeId;
      let req = {
        keyword: keyword,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      }
      if (storeId) {
        req['storeId'] = storeId;
      }
      goodsQuery(req).then(res => {
        if (res.code == 0) {
          this.data.goodList = [];
          this.setData({
            goodList: res.data.list
          });
          if (res.data.hasNextPage) { this.setData({ noData: true }) }
          console.log("this.data.goodList", this.data.goodList)
        }
      })
    },
    onLoad: function (options) {
      if (options.title) {
        this.setData({ title: options.title })
      }
      console.log('options', options)
      let temp = wx.getStorageSync('keywords');
      this.setData({
        keywordsList: temp
      });
      if (options.pageId) {
        this.data.pageId = options.pageId
        this.getPageConfigFunc(options.pageId)
      }
    },
    //获取页面配置
    getPageConfigFunc(pageId) {
      let params = {
        pageId: pageId
      };
      getPageConfig(params).then(res => {
        if (res.code == 0) {
          this.setData({
            modules: res.data.modules,
            title: res.data.config.name
          })
        }
      })
    },
    //点击取消按钮
    cancelFunc() {
      this.setData({ showKeywords: true, inputValue: "", goodList: [] })
    },
    //点击最近搜索
    handleRecently(e) {
      let text = e.currentTarget.dataset.item;
      this.setData({ inputValue: text });
      this.searchGoodsFunc(text)
      console.log('e', e);
    },
    goGoodsDetail(e) {
      console.log(e)
      let urlParams = {
        storeId: this.data.storeId,
        goodsSerial: e.currentTarget.dataset.item.goodsSerial
      }
      router.push({ name: "mall/goods-detail", params: urlParams })
    }
  },

})
